package com.mutouren.modules.org.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mutouren.common.entity.Result;
import com.mutouren.common.orm.spring.BaseServiceImpl;
import com.mutouren.common.utils.UUIDUtils;
import com.mutouren.modules.org.dao.AppDao;
import com.mutouren.modules.org.model.App;
import com.mutouren.modules.org.service.AppService;

@Service
public class AppServiceImpl extends BaseServiceImpl<App> implements AppService{

	@Resource
	private AppDao appDao;

	@Override
	public void afterPropertiesSet() throws Exception {
		this.baseDao = appDao;
	}

	@Override
	public void resetAppSecret(int appId) {
		String secret = UUIDUtils.createRandomUUID();
		this.appDao.setAppSecret(appId, secret);
	}
	
	@Override
	public Result register(App app) {
		Result result = new Result();
		result.setCode(1); // fail
		
		if (appDao.getByAppAlias(app.getAppAlias()) != null) return result;
		
		app.setAppSecret(UUIDUtils.createRandomUUID());
		appDao.insert(app);
		
		result.setCode(0); // success		
		return result;		
	}	
	
	@Override
	public int add(App app) {
		throw new RuntimeException("not permit direct call the add()");
	}

	@Override
	public App getByAppAlias(String appAlias) {
		return this.appDao.getByAppAlias(appAlias);
	}

}
